package com.xps.and.yuntong.Ui;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.os.Vibrator;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.app.abby.xbanner.AbstractUrlLoader;
import com.app.abby.xbanner.XBanner;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.xps.and.yuntong.Adapter.DingDanAdapter;
import com.xps.and.yuntong.Adapter.DingDanAdapter1;
import com.xps.and.yuntong.App;
import com.xps.and.yuntong.Data.bean1.OrderDriver;
import com.xps.and.yuntong.Data.bean1.SubmitJpush;
import com.xps.and.yuntong.Data.net.UserNetWorks;
import com.xps.and.yuntong.Entity.Dizhi;
import com.xps.and.yuntong.Entity.Dizhi1;
import com.xps.and.yuntong.R;
import com.xps.and.yuntong.Utils.AppCommond;
import com.xps.and.yuntong.Utils.GlobalPositionInfo;
import com.xps.and.yuntong.Utils.SPConstants;
import com.xps.and.yuntong.Utils.TtsUtil;
import com.xps.and.yuntong.View.Custom.WaveView;
import com.xps.and.yuntong.base.BaseActivity;
import com.xps.and.yuntong.service.RemoteService;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jude.utils.JUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import cn.jpush.android.api.JPushInterface;
import jp.wasabeef.glide.transformations.CropCircleTransformation;
import pl.droidsonroids.gif.GifImageView;
import rx.Subscriber;

/**
 * Created by administrator on 2017-12-12.
 */

public class ShouYeActivity extends BaseActivity implements View.OnClickListener {
    protected static final int MSG_WHAT = 0;
    protected static final int MSG_RECOVERY_CAMERA = 1;
    Handler timerHandler = null;
    private static Context context;
    private ImageView ivLeft, ivRight, iv_shangxian_and_xianxian;
    private ListView lv_dingdan;

    private XBanner banner;
    private List<String> gifUrls = new ArrayList<>();

    private List<String> list = new ArrayList();
    private List<OrderDriver.ReturnBodyBean.StudentInfoBean> adapterListData = new ArrayList();
    private String dingdan_hao;
    private String dingdan_id;
    private String dingdan_state;
    private DingDanAdapter dingDanAdapter;

    private int state = 0;
    private ArrayList<Dizhi1> dizhi1s = new ArrayList<>();
    private DingDanAdapter1 dingDanAdapter1;

    //侧栏
    public SlidingMenu menu;
    private ImageView iv_touxiang, iv_xing1, iv_xing2, iv_xing3, iv_xing4, iv_xing5;
    private TextView tv_name, tv_phone_number, tv_fenshu, tv_shangxian;
    private RelativeLayout rl_every_qiandao, rl_personal_ziliao, rl_my_qianbao, rl_dingdan_list, rl_xiugai_password, rl_more_shezhi, rl_about_us, rl_exit_login, rl_shangxian_and_xianxian;

    private com.xps.and.yuntong.Utils.RatingBar rb;
    //水波纹
    private WaveView mWaveView;

    MediaPlayer onlineMediaPlayer = null;
    MediaPlayer offlineMediaPlayer = null;


    //长连接广播
    private MyReceiver receiver = null;
    Vibrator vibrator;
    private String token = "";

    //监听网络状态
    BroadcastReceiver receiverq = null;
    AlertDialog normalDia;
    AlertDialog.Builder normalDialog;

    //电量连接
    private PowerManager powerManager = null;

    //测试jpush
    String tvStartLocationStr = "";
    String statlng = "";
    String statlat = "";
    String startTime;
    private String shebei = "";


    @Override
    protected int getLayout() {
        return R.layout.activity_shouye;
    }

    @Override
    protected void setData() {

        celanXianshi();
    }


    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_WHAT:
                if (JUtils.isNetWorkAvilable())
//                    Log.e("111", "aaa");
//                    wanwan();
                    if (timerHandler != null)
                        timerHandler.sendEmptyMessageDelayed(MSG_WHAT, 1000 * 5);
                break;
        }
        return super.handleMessage(msg);
    }


    @Override
    protected void onPause() {
        super.onPause();
        if (timerHandler != null) {
            timerHandler.removeCallbacksAndMessages(null);
            timerHandler = null;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        timerHandler = new Handler(getMainLooper(), this);
        timerHandler.sendEmptyMessageDelayed(MSG_WHAT, 1000);
        timerHandler.sendEmptyMessageDelayed(MSG_RECOVERY_CAMERA, 1000);
        SharedPreferences sp = JUtils.getSharedPreference();
        Intent intent = new Intent(ShouYeActivity.this, RemoteService.class);
        if (!TextUtils.isEmpty(sp.getString("state", ""))) {
            getOrderDriver();
            iv_shangxian_and_xianxian.setImageResource(R.drawable.yuan_shixin_lv_kuang);
            tv_shangxian.setText(R.string.xia_xian);
            lv_dingdan.setVisibility(View.VISIBLE);
            state = 1;
            mWaveView.start();
            onlineMediaPlayer.start();
            App.getInstance().startService(intent);

        } else {

            iv_shangxian_and_xianxian.setImageResource(R.drawable.yuan_shixin_red_kuang);
            tv_shangxian.setText(R.string.shang_xian);
            lv_dingdan.setVisibility(View.GONE);
            state = 0;
            doOffline();
            offlineMediaPlayer.start();
            mWaveView.stopImmediately();
            App.getInstance().stopService(intent);
        }

    }


    @Override
    protected void init(Bundle inBundle) {

        boolean login = JUtils.getSharedPreference().getBoolean("User_Login1", false);
        if (login) {
            startActivity(new Intent(ShouYeActivity.this, LoginActivity.class));
            finish();
        }
        slidingMenu();
        initview();
//        timerHandler = new Handler(getMainLooper(), this);
//        timerHandler.sendEmptyMessageDelayed(MSG_WHAT, 1000);
//        timerHandler.sendEmptyMessageDelayed(MSG_RECOVERY_CAMERA, 1000);
//        Intent intent = new Intent(ShouYeActivity.this, RemoteService.class);
        shebei = getDeviceBrand();
        if (!JUtils.getSharedPreference().getString("access_token", "").equals("")) {
            token = JUtils.getSharedPreference().getString("access_token", "").substring(0, 10);
            JPushInterface.setAliasAndTags(ShouYeActivity.this, token, null, null);
            //推给后台的极光url
            submitJpush();
            Log.e("111", token);
            Log.e("111", JUtils.getSharedPreference().getString("access_token", ""));
        }


        receiver = new MyReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.xps.and.yuntong.service.RemoteService");
        registerReceiver(receiver, filter);


//网路状态的展示
        normalDialog =
                new AlertDialog.Builder(ShouYeActivity.this);
        normalDialog.setTitle("温馨提示");
        normalDialog.setMessage("当前网络不可用联网后重试");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        startActivity(new Intent(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS));
                    }
                });
        normalDialog.setCancelable(false);
        normalDia = normalDialog.create();
        registerBroadrecevicer();

        if (!checkGPSIsOpen()) {
            final AlertDialog.Builder normalDialog =
                    new AlertDialog.Builder(ShouYeActivity.this);
            normalDialog.setTitle("温馨提示");
            normalDialog.setMessage("请打开GPS后再次进入");
            normalDialog.setPositiveButton("确定",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            startActivity(intent);
                            dialog.dismiss();
                        }
                    });
            normalDialog.setCancelable(false);
            normalDialog.show();
        }

        //电量优化，放开的时候再放开
//        powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            String packageName = "com.xps.and.yuntong";
//            boolean isIgnoring = powerManager.isIgnoringBatteryOptimizations(packageName);
//            if (!isIgnoring) {
//                Intent intent = new Intent(
//                        Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
//                intent.setData(Uri.parse("package:" + packageName));
//                try {
//                    startActivity(intent);
//                } catch (Exception ex) {
//                    ex.printStackTrace();
//                }
//            }
//        }
    }

    private void initview() {

        ivLeft = (ImageView) findViewById(R.id.iv_left);
        ivRight = (ImageView) findViewById(R.id.iv_right);
        iv_shangxian_and_xianxian = (ImageView) findViewById(R.id.iv_shangxian_and_xianxian);
        lv_dingdan = (ListView) findViewById(R.id.lv_dingdan);
        mWaveView = (WaveView) findViewById(R.id.wave_view);


        //侧栏
        iv_touxiang = (ImageView) findViewById(R.id.iv_touxiang);
        iv_xing1 = (ImageView) findViewById(R.id.iv_xing1);
        iv_xing2 = (ImageView) findViewById(R.id.iv_xing2);
        iv_xing3 = (ImageView) findViewById(R.id.iv_xing3);
        iv_xing4 = (ImageView) findViewById(R.id.iv_xing4);
        iv_xing5 = (ImageView) findViewById(R.id.iv_xing5);
        rb = (com.xps.and.yuntong.Utils.RatingBar) findViewById(R.id.rb);

        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_phone_number = (TextView) findViewById(R.id.tv_phone_number);
        tv_fenshu = (TextView) findViewById(R.id.tv_fenshu);
        tv_shangxian = (TextView) findViewById(R.id.tv_shangxian);

        rl_every_qiandao = (RelativeLayout) findViewById(R.id.rl_every_qiandao);
        rl_personal_ziliao = (RelativeLayout) findViewById(R.id.rl_personal_ziliao);
        rl_my_qianbao = (RelativeLayout) findViewById(R.id.rl_my_qianbao);
        rl_dingdan_list = (RelativeLayout) findViewById(R.id.rl_dingdan_list);
        rl_xiugai_password = (RelativeLayout) findViewById(R.id.rl_xiugai_password);
        rl_more_shezhi = (RelativeLayout) findViewById(R.id.rl_more_shezhi);
        rl_about_us = (RelativeLayout) findViewById(R.id.rl_about_us);
        rl_exit_login = (RelativeLayout) findViewById(R.id.rl_exit_login);
        rl_shangxian_and_xianxian = (RelativeLayout) findViewById(R.id.rl_shangxian_and_xianxian);

        ivLeft.setOnClickListener(this);
        ivRight.setOnClickListener(this);
        rl_shangxian_and_xianxian.setOnClickListener(this);

        rl_every_qiandao.setOnClickListener(this);
        rl_personal_ziliao.setOnClickListener(this);
        rl_my_qianbao.setOnClickListener(this);
        rl_dingdan_list.setOnClickListener(this);
        rl_xiugai_password.setOnClickListener(this);
        rl_more_shezhi.setOnClickListener(this);
        rl_about_us.setOnClickListener(this);
        rl_exit_login.setOnClickListener(this);

        List<String> imageUrls = new ArrayList<>();
        imageUrls.add("http://img2.tgbusdata.cn/v2/thumb/jpg/YzQ0NSw4MzAsNjQwLDQsMywxLC0xLDAscms1MA==/u/olpic.tgbusdata.cn/uploads/allimg/160324/238-160324160031.jpg");
        imageUrls.add("http://img2.tgbusdata.cn/v2/thumb/jpg/ZjliZCw4MzAsNjQwLDQsMywxLC0xLDAscms1MA==/u/olpic.tgbusdata.cn/uploads/allimg/160324/238-160324160031-50.jpg");
        imageUrls.add("http://img2.tgbusdata.cn/v2/thumb/jpg/OTg2YSw4MzAsNjQwLDQsMywxLC0xLDAscms1MA==/u/olpic.tgbusdata.cn/uploads/allimg/160324/238-160324160031-51.jpg");
        imageUrls.add("http://img2.tgbusdata.cn/v2/thumb/jpg/MmNjZSw4MzAsNjQwLDQsMywxLC0xLDAscms1MA==/u/olpic.tgbusdata.cn/uploads/allimg/160324/238-160324160031-52.jpg");
        imageUrls.add("http://img2.tgbusdata.cn/v2/thumb/jpg/MzQ2ZCw4MzAsNjQwLDQsMywxLC0xLDAscms1MA==/u/olpic.tgbusdata.cn/uploads/allimg/160324/238-160324160032.jpg");

        List<String> titles = Arrays.asList("This is the picture 1", "This is the picture 2", "This is the picture 3", "This is the picture 4", "This is the picture 5");
        banner = (XBanner) findViewById(R.id.banner);
        banner.setImageScaleType(ImageView.ScaleType.FIT_XY)
                .setImageUrls(imageUrls)
                .setImageLoader(new AbstractUrlLoader() {
                    @Override
                    public void loadImages(Context context, String url, ImageView image) {
                        Glide.with(context)
                                .load(url)
                                .into(image);
                    }

                    @Override
                    public void loadGifs(String url, GifImageView gifImageView, ImageView.ScaleType scaleType) {

                    }
                })
                .setBannerTypes(XBanner.CIRCLE_INDICATOR_TITLE)
                .setIndicatorGravity(XBanner.INDICATOR_START)
                .setTitles(titles)
                .setDelay(3000)
//                .setUpIndicators(R.drawable.ic_selected,R.drawable.ic_unselected)
                .setUpIndicatorSize(20, 20)
                .isAutoPlay(true)
                .start();


//        addShuJu();


        lv_dingdan.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (dingdan_state.equals("3")) {
                    Intent intent = new Intent(ShouYeActivity.this, FaceSignActivity.class);
                    intent.putExtra("dingdan_id", dingdan_id);
                    startActivity(intent);


                } else {

                    Intent intent = new Intent(ShouYeActivity.this, DingdanKanActivity.class);
//                    intent.putExtra("dingdan_id", dingdan_id);
                    startActivity(intent);
                }
//                App.getInstance().prepareYingYan();
            }
        });


        onlineMediaPlayer = MediaPlayer.create(App.getInstance(),
                R.raw.onlinevoice);
        offlineMediaPlayer = MediaPlayer.create(App.getInstance(),
                R.raw.offlinevoice);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:
                showLeftMenu();

                break;
            case R.id.iv_right:

//                Intent intent=new Intent(ShouYeActivity.this,DingdanKanActivity.class);
//                startActivity(intent);
                break;
            case R.id.rl_shangxian_and_xianxian:


                if (state == 1) {
                    iv_shangxian_and_xianxian.setImageResource(R.drawable.yuan_shixin_red_kuang);
                    tv_shangxian.setText(R.string.shang_xian);
                    lv_dingdan.setVisibility(View.GONE);
                    state = 0;
                    doOffline();
//                    App.getInstance().stopService(intent);

                    mWaveView.stopImmediately();
                    offlineMediaPlayer.start();

                    //此处需要刷新界面

                } else {
                    getOrderDriver();
                    iv_shangxian_and_xianxian.setImageResource(R.drawable.yuan_shixin_lv_kuang);
                    tv_shangxian.setText(R.string.xia_xian);
                    lv_dingdan.setVisibility(View.VISIBLE);
                    state = 1;
                    mWaveView.start();
                    onlineMediaPlayer.start();
                    doOnline();
                    //此处需要刷新界面

                }

                break;
            case R.id.rl_every_qiandao:
                //此处是为了测试接收jpush的
//                wanwan();
                break;
            case R.id.rl_personal_ziliao:

                Intent intent6 = new Intent(ShouYeActivity.this, PersonalZiliaoActivity.class);
                startActivity(intent6);
//                finish();

                break;
            case R.id.rl_my_qianbao:
                Intent intent3 = new Intent(ShouYeActivity.this, MyVolletActivity.class);
                startActivity(intent3);

                break;
            case R.id.rl_dingdan_list:
                Log.e("11", "11");
                Intent intent4 = new Intent(ShouYeActivity.this, DingdanListActivity.class);
                startActivity(intent4);
                break;
            case R.id.rl_xiugai_password:
                Intent intent1 = new Intent(ShouYeActivity.this, XiugaiPasswordActivity.class);
                startActivity(intent1);

                break;
            case R.id.rl_more_shezhi:
                Intent intent2 = new Intent(ShouYeActivity.this, MoreShezhiActivity.class);
                startActivity(intent2);


                break;
            case R.id.rl_about_us:
                Intent intent5 = new Intent(ShouYeActivity.this, AboutActivity.class);
                startActivity(intent5);
                break;
            case R.id.rl_exit_login:
                tuichu();
//              finish();
                break;


        }
    }

    //111111111
    private void slidingMenu() {

        menu = new SlidingMenu(this);
        menu.setMode(SlidingMenu.LEFT);
        // 设置触摸屏幕的模式
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
//        menu.setShadowWidthRes(R.dimen.shadow_width);
//        menu.setShadowDrawable(R.drawable.shadow);

        // 设置滑动菜单视图的宽度
        menu.setBehindOffsetRes(R.dimen.eieghty_margin);
        // 设置渐入渐出效果的值
        menu.setFadeDegree(0.35f);
        /**
         * SLIDING_WINDOW will include the Title/ActionBar in the content
         * section of the SlidingMenu, while SLIDING_CONTENT does not.
         */
        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        //为侧滑菜单设置布局
        menu.setMenu(R.layout.left_menu);
    }

    public void showLeftMenu() {
        if (menu != null) {
            menu.toggle();

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        banner.releaseBanner();
        if (receiver != null) {
            unregisterReceiver(receiver);
            receiver = null;
        }
        if (receiverq != null) {
            unregisterReceiver(receiverq);
            receiverq = null;
        }
        if (timerHandler != null) {
            timerHandler.removeCallbacksAndMessages(null);
            timerHandler = null;
        }
    }


    private void celanXianshi() {
        if (JUtils.getSharedPreference().getString("head_img", "") != null) {
            Glide.with(ShouYeActivity.this).load(JUtils.getSharedPreference().getString("head_img", ""))
                    .placeholder(R.drawable.logo)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)//禁用磁盘缓存
                    .skipMemoryCache(true)
                    .bitmapTransform(new CropCircleTransformation(ShouYeActivity.this))
                    .into(iv_touxiang);
        }
        if (JUtils.getSharedPreference().getString("name", "") != null) {
            tv_name.setText(JUtils.getSharedPreference().getString("name", ""));
        }
        if (JUtils.getSharedPreference().getString("mobile_phone", "") != null) {
            tv_phone_number.setText(JUtils.getSharedPreference().getString("mobile_phone", ""));
        }
        if (((JUtils.getSharedPreference().getInt("avg_score", -1)) + "") != null) {
            tv_fenshu.setText(JUtils.getSharedPreference().getInt("avg_score", -1) + "");
            rb.setRating(Long.parseLong(JUtils.getSharedPreference().getInt("avg_score", -1) + ""));
        }


    }


    //以下是长连接
    //下线
    void doOffline() {
        Intent intent = new Intent(ShouYeActivity.this, RemoteService.class);
        App.getInstance().stopService(intent);
        SharedPreferences sp = JUtils.getSharedPreference();
        SharedPreferences.Editor spEd = sp.edit();
        spEd.putString("state", "");
        spEd.commit();
        JUtils.getSharedPreference().edit().remove(SPConstants.WEBSOCKET_STARTED).commit();
    }

    //上线
    void doOnline() {
        Intent intent = new Intent(ShouYeActivity.this, RemoteService.class);
//        Intent intent1=getIntent();
//        intent.putExtra("userId", intent1.getStringExtra("userId"));
//        Log.e("111",intent1.getStringExtra("userId")+"");
        App.getInstance().startService(intent);
        SharedPreferences sp = JUtils.getSharedPreference();
        SharedPreferences.Editor spEd = sp.edit();
        spEd.putString("state", "true");
        spEd.commit();
    }

    public class MyReceiver extends BroadcastReceiver {


        @Override
        public void onReceive(Context context, Intent intent) {


//            vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
//            long[] pattern = {100, 400, 100, 400};   // 停止 开启 停止 开启
//            vibrator.vibrate(pattern, -1);
////            startAlarm();
//            TtsUtil.getInstance().startSpeaking("您有新的订单" + "");

        }
    }


    private void registerBroadrecevicer() {
        //获取广播对象
        receiverq = new IntenterBoradCastReceiver();
        //创建意图过滤器
        IntentFilter filter = new IntentFilter();
        //添加动作，监听网络
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(receiverq, filter);
    }


    //监听网络状态变化的广播接收器
    public class IntenterBoradCastReceiver extends BroadcastReceiver {
        private ConnectivityManager mConnectivityManager;
        private NetworkInfo netInfo;

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            String action = intent.getAction();
            if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                netInfo = mConnectivityManager.getActiveNetworkInfo();
                if (netInfo != null && netInfo.isAvailable()) {
                    if (netInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                        normalDia.dismiss();
                    } else if (netInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
                        normalDia.dismiss();
                    } else if (netInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                        normalDia.dismiss();
                    }
                } else {
                    normalDia.show();
                }
            }
        }
    }

    public void startAlarm() {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        if (notification == null) return;
        Ringtone r = RingtoneManager.getRingtone(ShouYeActivity.this, notification);
        r.play();
    }


    private boolean checkGPSIsOpen() {
        boolean isOpen;
        LocationManager locationManager = (LocationManager) this
                .getSystemService(Context.LOCATION_SERVICE);
        isOpen = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        return isOpen;
    }


    private void tuichu() {

        SharedPreferences sp = JUtils.getSharedPreference();
        SharedPreferences.Editor spEd = sp.edit();
        spEd.putInt("user_id", -1);
        spEd.putInt("sex", -1);
        spEd.putInt("driver_id", -1);
        spEd.putString("name", "");
        spEd.putString("mobile_phone", "");
        spEd.putString("access_token", "");
        spEd.putString("head_img", "");
        spEd.putString("balance", "");
        spEd.putString("idcard_id", "");
        spEd.putString("car_id", "");
        spEd.putBoolean("User_Login", false);
        spEd.commit();
        App.getInstance().stopTrackServer();
        Intent intent = new Intent(ShouYeActivity.this, LoginActivity.class);
        intent.putExtra("zhanghao", "2");
        startActivity(intent);
        Intent Serviceintent = new Intent(ShouYeActivity.this, RemoteService.class);
        stopService(Serviceintent);
        JPushInterface.setAliasAndTags(ShouYeActivity.this, "", null, null);
        finish();
    }

//    public void douserIdS() {
//        UserNetWorks.getHomeSelect("", new Subscriber<SelectBean>() {
//
//            @Override
//            public void onCompleted() {
//            }
//
//            @Override
//            public void onError(Throwable e) {
//                e.printStackTrace();
//            }
//
//            @Override
//            public void onNext(SelectBean selectBean) {
//                if (selectBean.getReturn_code().equals("SUCCESS")) {
//                    List<SelectBean.ReturnBodyBean> list = selectBean.getReturn_body();
//                    if (list != null && list.size() == 0) {
//                        SharedPreferences sp = JUtils.getSharedPreference();
//                        SharedPreferences.Editor spEd = sp.edit();
//                        spEd.putString("Access_token", "");
//                        spEd.putString("userId", "");
//                        spEd.putString("DriverId", "");
//                        spEd.putString("state", "");
//                        spEd.putString("headImg", "");
//                        spEd.putBoolean("User_Login", false);
//                        spEd.putString("SJ_balance", "");
//                        spEd.putString("SJ_enablePoint", "");
//                        spEd.putString("headImg", "");
//                        spEd.putString("name", "");
//                        spEd.putString("userPhone", "");
//                        spEd.putString("SJ_sex", "");
//                        spEd.putString("totalOnlineTime", "");
//                        spEd.putString("xingji", "");
//                        spEd.putString("SJ_age", "");
//                        spEd.commit();
//                        App.getInstance().stopTrackServer();
//                        Intent intent = new Intent(HomeActivity.this, LoginActivity.class);
//                        intent.putExtra("zhanghao", "2");
//                        startActivity(intent);
//                        Intent Serviceintent = new Intent(HomeActivity.this, RemoteService.class);
//                        stopService(Serviceintent);
//                        JPushInterface.setAliasAndTags(HomeActivity.this, "", null, null);
//                        finish();
//                    } else {
//                        ToastShow.getInstance(App.getInstance()).toastShow("有进行中订单,不能退出账号!");
//                    }
//                }
//            }
//        });
//    }


    private void getOrderDriver() {

        UserNetWorks.getOrederDiver("aaa", new Subscriber<OrderDriver>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
//                JUtils.Toast("网络错误");
            }

            @Override
            public void onNext(OrderDriver orderDriver) {


                if (orderDriver.getReturn_body() == null) {
                    JUtils.Toast(orderDriver.getReturn_msg() + "");
                } else {
                    Log.e("111", orderDriver.getReturn_body().getOrderInfo() + "");
                    List<OrderDriver.ReturnBodyBean.StudentInfoBean> list = orderDriver.getReturn_body().getStudentInfo();
                    OrderDriver.ReturnBodyBean.OrderInfoBean orderInfoBean = orderDriver.getReturn_body().getOrderInfo();
                    Log.e("111", list.toString() + "");

                    if (list != null && list.size() <= 0) {
                        return;
                    }
                    adapterListData = list;
                    dingdan_hao = orderDriver.getReturn_body().getOrderInfo().getOrder_sn();
                    dingdan_id = orderDriver.getReturn_body().getOrderInfo().getOrder_id();
                    dingdan_state = orderDriver.getReturn_body().getOrderInfo().getOrder_state();
                    dingDanAdapter = new DingDanAdapter(ShouYeActivity.this, orderInfoBean, adapterListData);
                    Log.e("111", "====adapterListData" + adapterListData + "dingdan_hao" + dingdan_hao);
                    lv_dingdan.setAdapter(dingDanAdapter);
                }


//                if (orderDriver.getReturn_msg().equals("当前没有进行中订单")) {
//
//
//                } else if (orderDriver.getReturn_msg().equals("获取成功")) {
//
//                }

            }
        });


    }

    private void submitJpush() {
        UserNetWorks.SubmitJpush(token, shebei, new Subscriber<SubmitJpush>() {
            @Override
            public void onCompleted() {


            }

            @Override
            public void onError(Throwable e) {

                JUtils.Toast("网络错误");
            }

            @Override
            public void onNext(SubmitJpush submitJpush) {
                Log.e("111", submitJpush.getReturn_msg() + "极光");

            }
        });


    }

    private String getDeviceBrand() {
        return android.os.Build.BRAND;
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(AppCommond a) {
        if (!AppCommond.check(a)) return;
        for (String activityName : a.getAcNameList()) {
            if (activityName.contains(getClass().getSimpleName())) {
                unRegEventBus();
                finish();
                break;
            }
        }
    }

    void unRegEventBus() {
        if (EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().unregister(this);
    }

}
